package com.itany.corejava.code09_集合.map集合;

import com.itany.corejava.code08_常用类.包装类.Stu;

import java.util.*;

import  java.util.Map.Entry;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年11月21日 16:08
 */
public class Test03_遍历 {
    public static void main(String[] args) {
        Map<Integer, Student> map = new HashMap<>();
        Student student1 = new Student(1, "admin");
        Student student2 = new Student(2, "alice");
        Student student3 = new Student(3, "jack");
        map.put(1, student1);
        map.put(2, student2);
        map.put(3, student3);
        System.out.println(map);
        // test01(map);
        // test02(map);

        test03(map);

    }

    /**
     * 通过keySet()获取所有的key(键)的集合，然后要遍历所有的key
     *
     * @param map 要遍历的map
     */
    public static void test01(Map<Integer, Student> map) {
        Set<Integer> keys = map.keySet();
        // System.out.println(keys);
        for (Integer key : keys) {
            System.out.println("key:" + key + ",value:" + map.get(key));
        }
    }

    /**
     * 通过values获取所有的value(值)的集合
     *
     * @param map
     */
    public static void test02(Map<Integer, Student> map) {
        Collection<Student> values = map.values();
        for (Student student : values) {
            System.out.println(student);
        }
    }

    /**
     * 通过entrySet()获取key-value的集合，然后遍历所有的key-value键值对
     *
     * @param map
     */
    public static void test03(Map<Integer, Student> map) {
        // Set<Map.Entry<Integer, Student>> entries = map.entrySet();
        // Iterator<Map.Entry<Integer, Student>> it = entries.iterator();
        // while (it.hasNext()) {
        //     Map.Entry<Integer, Student> entry=it.next();
        //     System.out.println("key:" + entry.getKey() + "，value:" +entry.getValue());
        // }

        // 也可以手动导包
        Set<Entry<Integer,Student>> entries=map.entrySet();
        Iterator<Entry<Integer,Student>> it=entries.iterator();
        while(it.hasNext()){
            Entry<Integer, Student> entry=it.next();
            System.out.println("key:" + entry.getKey() + "，value:" +entry.getValue());
        }



    }
}
